package com.zz.yt.test.delegate.net;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.whf.android.jar.base.delegate.BaseDelegate;
import com.whf.android.jar.util.FormatUtil;
import com.whf.android.jar.util.log.LatteLogger;
import com.zz.yt.test.R;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间戳转换问题
 */
public class SjcDelegate extends BaseDelegate {

    final long deliveryPromiseTime = 1701345600000L;

    @NonNull
    public static SjcDelegate newInstance() {

        Bundle args = new Bundle();

        SjcDelegate fragment = new SjcDelegate();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object setLayout() {
        return R.layout.activity_sjc;
    }

    @Override
    protected void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        setOnClickListener(R.id.id_btn_calendar, v -> {
            if (ObjectUtils.isNotEmpty(deliveryPromiseTime)) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(deliveryPromiseTime);

                if (FormatUtil.isYesterday(calendar.getTime())) {
                    setText(R.id.id_text_calendar, "昨天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
                } else if (FormatUtil.isToday(calendar.getTime())) {
                    setText(R.id.id_text_calendar, "今天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
                } else if (FormatUtil.isTomorrow(calendar.getTime())) {
                    setText(R.id.id_text_calendar, "明天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
                } else if (FormatUtil.isAcquired(calendar.getTime())) {
                    setText(R.id.id_text_calendar, "后天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
                } else {
                    setText(R.id.id_text_calendar, FormatUtil.date2String(calendar.getTime(), "yyyy-MM-dd HH:mm:ss"));
                }
            }
        });

        setOnClickListener(R.id.id_btn_date, v -> {
            if (ObjectUtils.isNotEmpty(deliveryPromiseTime)) {
                Date date = TimeUtils.millis2Date(deliveryPromiseTime);

                if (FormatUtil.isYesterday(date)) {
                    setText(R.id.id_text_date, "昨天 " + FormatUtil.date2String(date, "HH:mm"));
                } else if (FormatUtil.isToday(date)) {
                    setText(R.id.id_text_date, "今天 " + FormatUtil.date2String(date, "HH:mm"));
                } else if (FormatUtil.isTomorrow(date)) {
                    setText(R.id.id_text_date, "明天 " + FormatUtil.date2String(date, "HH:mm"));
                } else if (FormatUtil.isAcquired(date)) {
                    setText(R.id.id_text_date, "后天 " + FormatUtil.date2String(date, "HH:mm"));
                } else {
                    setText(R.id.id_text_date, FormatUtil.date2String(date, "yyyy-MM-dd HH:mm:ss"));
                }
            }
        });

        setOnClickListener(R.id.id_btn_nd, v -> {
            String string = "2023-12-31 08:40:00";
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(FormatUtil.string2Date(string));
            if (FormatUtil.isYesterday(calendar.getTime())) {
                setText(R.id.id_text_nd, "昨天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
            } else if (FormatUtil.isToday(calendar.getTime())) {
                setText(R.id.id_text_nd, "今天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
            } else if (FormatUtil.isTomorrow(calendar.getTime())) {
                setText(R.id.id_text_nd, "明天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
            } else if (FormatUtil.isAcquired(calendar.getTime())) {
                setText(R.id.id_text_nd, "后天 " + FormatUtil.date2String(calendar.getTime(), "HH:mm"));
            } else {
                setText(R.id.id_text_nd, FormatUtil.date2String(calendar.getTime(), "yyyy-MM-dd HH:mm:ss"));
            }

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                int year = calendar.get(java.util.Calendar.YEAR);
                int month = calendar.get(java.util.Calendar.MONTH) + 1;
                int day = calendar.get(java.util.Calendar.DATE);
                LatteLogger.i(year + ";" + month + ";" + day + ";" + calendar.getWeekYear()
                        + ";" + calendar.getWeeksInWeekYear());
            }
        });
    }

}
